/**
 * @file iwdg.c
 * @author Victor Lau (1345236485@qq.com)
 * @brief 
 * @version 0.1
 * @date 2023-01-06
 * 
 * @copyright Copyright (c) 2023 Victor Lau. All rights reserved.
 * 
 */
#include "iwdg.h"
#include "at32f421_wdt.h"

void iwdg_init()
{
    /* disable register write protection */
    wdt_register_write_enable(TRUE);

    /* set the wdt divider value */
    wdt_divider_set(WDT_CLK_DIV_4);

    /* set reload value

    timeout = reload_value * (divider / lick_freq )    (s)

    lick_freq    = 40000 Hz
    divider      = 4
    reload_value = 3000

    timeout = 3000 * (4 / 40000 ) = 0.3s = 300ms
    */
    wdt_reload_value_set(20000 - 1);

    /* reload wdt counter */
    wdt_counter_reload();

    /* enable wdt */
    wdt_enable();
}
